package org.apache.ibatis.add;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.junit.platform.commons.util.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author: ShipTang
 * @Description: 自定义类型处理器
 * @Date: 2021/11/26 17:01
 */
public class CustomerTypeHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        //写入
        ps.setString(i, parameter);
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String columnValue = rs.getString(columnName);
        System.out.println(columnName+"="+columnValue);
        if (StringUtils.isBlank(columnValue)) {
            return "";
        } else {
            return columnValue;
        }
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String columnValue = rs.getString(columnIndex);
        if (StringUtils.isBlank(columnValue)) {
            return "";
        } else {
            return columnValue;
        }
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String columnValue = cs.getString(columnIndex);
        if (StringUtils.isBlank(columnValue)) {
            return "";
        } else {
            return columnValue;
        }
    }
}
